import time

from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from common.base import Base
from selenium.webdriver.common.by import By

class CommunityPage(Base):
    def community_click(self):
        self.tap(x=490,y=1500)

    def plate_click(self):
        self.click(('xpath','//android.widget.TextView[@text="版块"]'))

    def plate_game_click(self):
        b = self.find_elements(('id','com.nearme.gamecenter:id/iv_icon'))
        b[3].click()

    def subscribe_click(self):
        self.click(('xpath','//android.widget.Button[@text="订阅"]'))

    def get_game_name(self):
        return self.get_text(('id','com.nearme.gamecenter:id/board_name'))

    def back_button_click(self):
        self.click(('id','com.nearme.gamecenter:id/iv_actionbar_back_icon'))

    def get_subscribe_name(self):
        return self.get_text(('id','com.nearme.gamecenter:id/name'))

    def more_click(self):
        self.click(('xpath','//android.widget.TextView[@text="更多"]'))

    def topic_click(self):
        self.click(('id','com.nearme.gamecenter:id/shade'))

    def optReply_click(self):
        self.tap(x=200,y=1430)

    def input_content(self,content):
        self.clear(('id','com.nearme.gamecenter:id/post_edit_text'))
        self.send_keys(('id','com.nearme.gamecenter:id/post_edit_text'),content)

    def submit_click(self):
        self.click(('id','com.nearme.gamecenter:id/btn_submit'))

    def screen_press(self):
        for i in range(7):
            self.slide_up()

    def get_user_text(self):
        return self.get_text(('xpath','//android.view.View[@text="真好啊"]'))

    def release_button_click(self):
        self.click(('id','com.nearme.gamecenter:id/nx_floating_button_main_fab'))

    def post_button_click(self):
        self.click(('id','com.nearme.gamecenter:id/nx_floating_button_child_fab'))

    def content_send_keys(self,content):
        self.click(('id','com.nearme.gamecenter:id/post_msg_content_tip_tv'))
        self.clear(('id','com.nearme.gamecenter:id/post_msg_content_tip_tv'))
        self.send_keys(('id','com.nearme.gamecenter:id/post_msg_content_tip_tv'),content)

    def check_release(self):
        self.click(('id','com.nearme.gamecenter:id/board_name'))
        self.click(('id','com.nearme.gamecenter:id/board_icon'))

    def release_submit(self):
        self.click(('id','com.nearme.gamecenter:id/btn_submit'))

    def get_toast_text(self):
        driver = self.driver
        toast = WebDriverWait(driver, 10, 0.1).until(
            EC.presence_of_element_located((By.XPATH, "//*[contains(@text,'发布成功，你可能要火了')]")))
        return toast.text


if __name__ == '__main__':
    community = CommunityPage(appPackage='com.nearme.gamecenter', appActivity='.ui.activity.SplashActivity')
    time.sleep(5)
    community.community_click()
    community.release_button_click()
    community.post_button_click()
    community.content_send_keys("测试文本1")
    community.check_release()
    time.sleep(3)
    community.release_submit()
    print(community.get_toast_text())



    # community.more_click()
    # community.topic_click()
    # time.sleep(5)
    # community.screen_press()
    # community.optReply_click()
    # community.input_content("真好啊")
    # community.submit_click()
    # time.sleep(3)
    # print(community.get_user_text())

    # community.plate_click()
    # community.plate_game_click()
    # community.subscribe_click()
    # print(community.get_game_name())
    # community.back_button_click()
    # print(community.get_subscribe_name())
    time.sleep(5)
    # community.quit()